package com.onecar.carplatform.jtt.handler.jtt808;

import com.onecar.carplatform.common.constants.Jt808MsgId;
import com.onecar.carplatform.common.protocol.basics.JtMessage;
import com.onecar.carplatform.common.protocol.jt808.Jt808Msg0100;
import com.onecar.carplatform.common.protocol.jt808.Jt808Msg8100;
import com.onecar.carplatform.jtt.handler.base.JttMsgHandlerBase;
import com.onecar.carplatform.jtt.handler.base.JttMsgHandlerDesc;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author : ljq
 * @Description :
 * @Date : 2021/8/23 11:36
 * @Version : 1.0
 */
@Slf4j
@JttMsgHandlerDesc(msgId = Jt808MsgId.TERMINAL_REGISTER)
public class Jtt808Msg0100Handler extends JttMsgHandlerBase {
    @Override
    public void channelRead0(ChannelHandlerContext context, JtMessage jtMessage) throws Exception {
        if (!(jtMessage instanceof Jt808Msg0100)) {
            log.error("is not instanceof Jt808Msg0100 msgId {}", jtMessage.getHeader().getMsgId());
            return;
        }

        Jt808Msg8100 jt808Msg8100 = new Jt808Msg8100();
        jt808Msg8100.setHeader(getRespHandler(jtMessage));
        jt808Msg8100.getHeader().setMsgId(Jt808MsgId.TERMINAL_REGISTER_RESPONSE.getMsgId());

        jt808Msg8100.setResponseSerialId(jtMessage.getHeader().getMsgSerialNumber());
        jt808Msg8100.setResult((byte) 0);
        jt808Msg8100.setAuthCode("123456789");

        log.debug("===========> send back jtMessage:{}", jt808Msg8100);

        context.writeAndFlush(jt808Msg8100).addListener(future -> {
            if (!future.isSuccess()) {
                log.error("发送失败", future.cause());
            }
        });
    }
}
